﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Collections;
using ProgNetComponents.Interfaces;
using System.ComponentModel;
using System.Drawing.Design;
using ProgNetComponentsDx.Options.Design;
using System.Drawing;
using ProgNetComponentsDx.Base.Components;
using ProgNetComponents.Controls;

namespace ProgNetComponentsDx.Options
{
    [TypeConverter(typeof(OptionTypeConverter))]
    public class Option : ProgNetComponents.Classes.BaseCustomObject, INamedObject
    {
        internal static Color Color
        {
            get { return Color.Aqua; }
        }
        private string _Name;
        private Type _OptionType;
        private object _DefaultValue;

        public Option()
        {

        }
        public Option(string name) : this(name, null)
        {

        }
        public Option(string name, object defaultvalue)
        {
            Name = name;
            DefaultValue = defaultvalue;
        }
        public Option(string name, Type type)
        {
            Name = name;
            OptionType = type;
        }
        public Option(string name, object defaultvalue, object BrowserId)
        {
            Name = name;
            DefaultValue = defaultvalue;
            this.BrowserId = BrowserId;
        }
        public Option(string name, Type type, object BrowserId)
        {
            Name = name;
            OptionType = type;
            this.BrowserId = BrowserId;
        }
        public string Name
        {
            get { return _Name; }
            set { _Name = ProgNetComponents.Tools.ConstHelper.GetFriendlyName(value); }
        }

        [Editor(typeof(ProgNetComponents.Design.Editors.ObjectEditor), typeof(UITypeEditor))]
        public object DefaultValue
        {
            get { return _DefaultValue; }
            set
            {
                _DefaultValue = value;
                if (value != null)
                {
                    _OptionType = value.GetType();
                }
            }
        }

        [Editor(typeof(OptionTypeEditor), typeof(UITypeEditor))]
        public Type OptionType
        {
            get { return _OptionType; }
            set
            {
                _OptionType = value;
                if (_DefaultValue != null)
                {
                    if (value == typeof(DateTime))
                    {
                        try
                        {
                            _DefaultValue = Convert.ChangeType(_DefaultValue, value);
                        }
                        catch
                        {
                            _DefaultValue = DateTime.Today;
                        }
                    }
                    else if (value != null)
                    {
                        try
                        {
                            _DefaultValue = Convert.ChangeType(_DefaultValue, value);
                        }
                        catch
                        {
                            try
                            {
                                _DefaultValue = value.IsValueType ? Activator.CreateInstance(value) : null;
                            }
                            catch
                            {
                                _DefaultValue = null;
                            }
                        }
                    }
                }
            }
        }

        [Editor(typeof(ProgNetComponentsDx.Browsers.Design.Editors.BrowserNameEditor), typeof(UITypeEditor))]
        public object BrowserId { get; set; }

        public bool ValidateValue(object newValue)
        {
            if (BrowserId != null)
            {
                return Browsers.BrowserCache.IsValid(BrowserId, true, newValue);
            }
            return true;
        }
    }

    [Editor(typeof(OptionCollectionEditor), typeof(UITypeEditor))]
    public class OptionCollection : BaseNamedCollection<Option>
    {

    }

    public abstract class OptionNames
    {

    }

    [Designer(typeof(ProgNetComponents.Design.Designers.CollectionComponentDesigner))]
    public class OptionContainer : Component
    {
        private OptionGroupCollection _Items = new OptionGroupCollection();

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public OptionGroupCollection Items
        {
            get { return _Items; }
        }
    }
}
